/*
 * ErrorDialog.java
 *
 * Created on September 8, 2005, 10:55 AM
 */
package com.bff.mpd.musicjukebox.ui;

import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.actions.PlaylistInsertAlbumAction;
import com.bff.mpd.musicjukebox.ui.components.MusicJDialog;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.mpd.utils.Utils;
import java.awt.Frame;
import java.awt.Dimension;
import javax.swing.JDialog;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.objects.MPDAlbum;

/**
 *
 * @author  Bill
 */
public class AlbumThumbnailDialog extends MusicJDialog {

    private MPDAlbum album;
    private MPDPlaylist playlist;
    private SongListTablePanel songListTablePanel;
    private final SplashScreen sp;

    /** Creates new form ErrorDialog */
    public AlbumThumbnailDialog(final Frame frame, final MPDAlbum album) {
        super(frame);

        final JDialog dialog = this;

        sp = new SplashScreen(Utils.getParentComponent(this), false);
        sp.setIndeterminate(true);
        sp.setTitleLabel(Utils.getMessage("PROGRESS_LOADING_ALBUM"));
        sp.setMainTextArea("\n" + Utils.getMessage("PROGRESS_LOADING_ALBUM_TEXT", new Object[] {album.getName()}) + "\n\n");
        sp.setProgressBarLabel(Utils.getMessage("PROGRESS_LOADING_ALBUM_LABEL"));
        sp.pack();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    setTitle(Utils.getMessage("ALBUM_DIALOG_TITLE",new Object[] {album.getName()}));
                    setAlbum(album);

                    setPlaylist(JukeboxProperties.getInstance().getPlaylist());

                    songListTablePanel = new SongListTablePanel();

                    listSongs();

                    initComponents();
                    add(songListTablePanel);

                    setSize(new Dimension(750, 500));

                    Utils.centerInParent(dialog, frame);
                    sp.setStopped(true);
                    setVisible(true);
                } finally {
                    if (!sp.isStopped()) {
                        sp.setStopped(true);
                    }
                }
            }
        }).start();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnPanel = new BJJPanel();
        btnAddAlbum = new BJJButton();
        btnClose = new BJJButton();
        lblMessage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(Utils.getMessage("ALBUM_DIALOG_TITLE", new Object[] {album.getName()})); // NOI18N

        btnPanel.setLayout(new java.awt.GridBagLayout());

        btnAddAlbum.setAction(new PlaylistInsertAlbumAction(playlist,getAlbum()));
        btnAddAlbum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddAlbumActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnAddAlbum, gridBagConstraints);

        btnClose.setText(Utils.getMessage("DIALOG_CLOSE")); // NOI18N
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnClose, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 6;
        btnPanel.add(lblMessage, gridBagConstraints);

        getContentPane().add(btnPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

    private void btnAddAlbumActionPerformed(java.awt.event.ActionEvent evt) {
        lblMessage.setText(Utils.getMessage("ALBUM_DIALOG_LBL_ADDED", new Object[] {album.getName()}));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddAlbum;
    private javax.swing.JButton btnClose;
    private javax.swing.JPanel btnPanel;
    private javax.swing.JLabel lblMessage;
    // End of variables declaration//GEN-END:variables

    public MPDAlbum getAlbum() {
        return album;
    }

    public void setAlbum(MPDAlbum album) {
        this.album = album;
    }

    public MPDPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(MPDPlaylist playlist) {
        this.playlist = playlist;
    }

    private void listSongs() {
        try {
            getSongListTablePanel().setAlbumSongs(getAlbum());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private SongListTablePanel getSongListTablePanel() {
        return (SongListTablePanel) songListTablePanel;
    }
}
